describe("防疫-地图管理", () => {
  beforeEach(() => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);
  });

  it.only("进入楼栋详情-页面显示", () => {
    getBuildingDetails();

    cy.contains("电梯信息");
    // 判断页面表头信息是否正确
    let tableTh = ["编号", "名称", "所在楼层", "地图预览", "操作"];
    cy.get("table>thead>tr>th").should("have.length", tableTh.length);
    for (let i = 0; i < tableTh.length; i++) {
      cy.contains(tableTh[i]);
    }
    // 判断表格数据条数是否大于0
    cy.get("table>tbody>tr").then(($tr) => {
      expect($tr.length).to.greaterThan(0);
    });
  });

  it("添加地图", () => {
    getBuildingDetails();
    cy.contains("添加地图").click();
    getElementBtn("地图名称").type("测试1");
    getElementBtn("所在楼层").type("18");
    cy.getByData("choose-btn").click();
    // 上传文件
    cy.get('input[type="file"]').selectFile(
      {
        contents: "cypress/fixtures/test-map1.png",
      },
      { force: true }
    );
    cy.intercept("POST", "/api/robot-disinfect-map/v1/add").as("addMap");
    cy.contains("确定").click();
    cy.wait("@addMap");
  });

  it("编辑地图", () => {
    getBuildingDetails();
    cy.get("table>tbody>tr").eq(-1).contains("编辑").click();
    getElementBtn("地图名称").clear();
    getElementBtn("地图名称").type("测试2");
    getElementBtn("所在楼层").clear();
    getElementBtn("所在楼层").type("16");
    cy.getByData("choose-btn").click();
    // 上传文件
    cy.get('input[type="file"]').selectFile(
      {
        contents: "cypress/fixtures/test-map2.png",
      },
      { force: true }
    );
    cy.intercept("PUT", "/api/robot-disinfect-map/v1/update").as("updateMap");
    cy.contains("确定").click();
    cy.wait("@updateMap");
  });

  it("删除地图", () => {
    getBuildingDetails();
    cy.intercept("DELETE", "/api/robot-disinfect-map/v1/*").as("deleteMap");

    cy.get("table>tbody>tr").eq(-1).contains("删除").click();

    cy.contains("确认").click();
    cy.wait("@deleteMap");
    cy.contains("删除成功");
  });

  it("进入电梯-页面显示", () => {
    getElevatorDetails();
    // 判断页面表头信息是否正确
    let tableTh = ["编号", "名称", "App ID", "Project ID", "公司名称", "操作"];
    cy.get("table>thead>tr>th").should("have.length", tableTh.length);
    for (let i = 0; i < tableTh.length; i++) {
      cy.contains(tableTh[i]);
    }
    // 判断表格数据条数是否大于0
    cy.get("table>tbody>tr").then(($tr) => {
      expect($tr.length).to.greaterThan(0);
    });
  });

  it("添加电梯", () => {
    getElevatorDetails();
    cy.contains("添加配置").click();
    getElementBtn("配置名称").type("电梯1");
    getElementBtn("App ID").type("123");
    getElementBtn("Project ID").type("12345");
    getElementBtn("App Secret").type("123");
    getElementBtn("App Code").type("456");
    getElementBtn("机器人 ID").type("10");

    getElementBtn("公司名称").click();
    // 判断数据是否大于0
    cy.getByData("company-name-item").should(($li) => {
      expect($li.length).to.be.greaterThan(0);
    });

    cy.getByData("company-name-item").first().click();

    cy.intercept("POST", "/api/itlcloud-api/v1/create").as("createElevator");

    cy.contains("确定").click();
    cy.contains("确认继续操作吗")
      .closest(".el-message-box")
      .contains("确定")
      .click();
    cy.wait("@createElevator");
    cy.contains("操作成功");
  });

  it("编辑电梯", () => {
    getElevatorDetails();
    cy.contains("编辑").click();
    getElementBtn("配置名称").clear();
    getElementBtn("配置名称").type("1");
    getElementBtn("App ID").clear();
    getElementBtn("App ID").type("1525039809096454145");
    getElementBtn("Project ID").clear();
    getElementBtn("Project ID").type("00001842");
    getElementBtn("App Secret").clear();
    getElementBtn("App Secret").type("104F6AA813C1EE25");
    getElementBtn("App Code").clear();
    getElementBtn("App Code").type("00002V3");

    getElementBtn("公司名称").click();
    // 判断数据是否大于0
    cy.getByData("company-name-item").should(($li) => {
      expect($li.length).to.be.greaterThan(0);
    });

    cy.getByData("company-name-item").first().click();

    cy.intercept("PUT", "/api/itlcloud-api/v1/update").as("updateElevator");

    cy.contains("确定").click();
    cy.contains("确认继续操作吗")
      .closest(".el-message-box")
      .contains("确定")
      .click();
    cy.wait("@updateElevator");
    cy.contains("操作成功");
  });

  it("查看电梯-同步电梯", () => {
    getElevatorDetails();
    cy.intercept("GET", "/api/elevator/v1/list*").as("getElevatorList");
    cy.contains("查看电梯").click();
    cy.wait("@getElevatorList");
    // 判断页面表头信息是否正确
    let tableTh = ["编号", "设备名称", "设备编码", "设备类型"];
    cy.get("table>thead>tr>th").should("have.length", tableTh.length);
    for (let i = 0; i < tableTh.length; i++) {
      cy.contains(tableTh[i]);
    }
    // 判断表格数据条数是否大于0
    cy.get("table>tbody>tr").then(($tr) => {
      expect($tr.length).to.greaterThan(0);
    });
    cy.intercept("GET", "/api/elevator/v1/syncElevator/*").as(
      "synchronizationElevator"
    );
    cy.contains("同步电梯").click();
    cy.wait("@synchronizationElevator");
  });

  context("查询搜索", () => {
    it.only("有数据", () => {
      getElevatorDetails();
      cy.contains("button", "重置查询").click();
      cy.get('input[placeholder="编号"]').type("3");
      // cy.get('input[placeholder="App ID"]');
      // cy.get('input[placeholder="Project ID"]');
      cy.intercept("GET", "/api/itlcloud-api/v1/list*").as("searchElevator");
      cy.contains("button", "查询搜索").click();
      cy.wait("@searchElevator");
      cy.get("table>tbody>tr>td").first().should("have.text", "3");
    });

    it.only("无数据", () => {
      getElevatorDetails();
      cy.contains("button", "重置查询").click();
      cy.get('input[placeholder="名称"]').type("2");
      cy.intercept("GET", "/api/itlcloud-api/v1/list*").as("searchElevator");
      cy.contains("button", "查询搜索").click();
      cy.wait("@searchElevator");
      cy.contains("暂无数据");
    });
  });
});

function getBuildingDetails() {
  cy.intercept("GET", "/api/building/v1/list*").as("getBuildingList");
  cy.visit("/disinfect/map-management/building");
  cy.wait("@getBuildingList");
  cy.getByData("building-item").then(($item) => {
    expect($item.length).to.be.greaterThan(0);
  });
  cy.intercept("GET", "/api/building/v1/detail/*").as("getDetailContent");
  cy.intercept("GET", "/api/robot-disinfect-map/v1/list/*").as(
    "getDisinfectMapList"
  );
  cy.getByData("building-item").first().click();
  cy.wait("@getDetailContent");
  cy.wait("@getDisinfectMapList");
}

function getElementBtn(label) {
  return cy.contains("label", label).siblings();
}

function getElevatorDetails() {
  cy.intercept("GET", "/api/itlcloud-api/v1/list*").as(
    "getElevatorManagementList"
  );
  cy.visit("/disinfect/map-management/elevator");
  cy.wait("@getElevatorManagementList");
}
